package com.njxzc.igo.service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.Map.Entry;

import com.njxzc.igo.Dao.ShoppingCartDao;
import com.njxzc.igo.Dao.ShoppingCartDaoImpl;
import com.njxzc.igo.Dao.UserDao;
import com.njxzc.igo.Dao.UserDaoImpl;
import com.njxzc.igo.model.ShoppingCart;
import com.njxzc.igo.model.User;
import com.njxzc.igo.utils.AiGouUtil;

public class ShoppingCartServiceImpl implements ShoppingCartService {

	private ShoppingCartDao scd = new ShoppingCartDaoImpl();
	private UserDao ud = new UserDaoImpl();
	public void addGoodsInCart(String username, long productId, int amount) {
			
			User user = ud.findByName(username);
			List<ShoppingCart> list = scd.findCartByUsernameAndProdId(username, productId);
			if(list.size()==0){
				scd.saveShoppingCart(user, productId, amount);
				return;
			}
				scd.updateShoppingCart(list.get(0),amount);
	}
	@Override
	public List<ShoppingCart> loadmyCart(String username) {
		
		User user = ud.findByName(username);
		Set<ShoppingCart> shoppingcart = user.getShoppingcarts();
		List<ShoppingCart> list=scd.loadMyShoppingCart(shoppingcart);
		return list;
	}
	@Override
	public Map<String,List<ShoppingCart>> GroupBySales(List<ShoppingCart> shoppingcart) {
		return AiGouUtil.groupBy(shoppingcart);
	}
	@Override
	public void ModifyShoppingCartById(long productId,long userId, int amount) {
		scd.updateShoppingCart(productId, userId, amount);
	}
	
	
	
	public void saveUserShoppingCart(Map map) {
		scd.saveOrUpdateAll(map);
	}
	@Override
	public void deleteInShoppingCart(String shoppingcardId) {
		scd.deleteShoppingcartById(shoppingcardId);
		//this.deleteShoppingCartInMap(map, shoppingcardId);
	}
/*	
	public void deleteShoppingCartInMap(Map map,String shoppingcardId){
		Iterator it = map.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry entry = (Map.Entry)it.next();
			List<ShoppingCart> temp = (List<ShoppingCart>) entry.getValue();
			for(ShoppingCart cart :temp){
				if(cart.getShoppingCartId().equals(shoppingcardId)){
					temp.remove(cart);
					break;
				}
			}
		}
	}
	*/
	@Override
	public void deleteChoosenItems(List idList) {
		scd.deleteByBatch(idList);
	}
	@Override
	public List<ShoppingCart> loadSelectedShoppingCart(List shoppingCartIds) {
		List<ShoppingCart> temp =  scd.findAllInIds(shoppingCartIds);
		return scd.loadMyShoppingCart(temp);
	}

	public void RemoveFromShoppingCart(String[] orderShoppingCartIds) {
		scd.deleteFromIds(orderShoppingCartIds);
		
	}
	@Override
	public int CountShoppingCart(Map map) {
		if(map==null||map.size()==0){
			return 0 ;
		}
		
		int i = 0;
		Iterator<Map.Entry<String, List<ShoppingCart>>> it = map.entrySet().iterator();
		while(it.hasNext()){
		Map.Entry<String,List<ShoppingCart>> entry = (Entry<String,List<ShoppingCart>>) it.next();
		List<ShoppingCart> list = (List<ShoppingCart>) entry.getValue();
		for(ShoppingCart sc :list){
			i++;
		}
	}
		return i;
	}
}
